package com.liy.framework.common.redis.utils;

import java.util.*;
import java.io.*;

public class RedisKeyUtils
{
    public static final String TOKEN = "TK:";
    public static final String USER_MENU = "UM:";
    public static final String USER_PERMISSION = "UP:";
    public static final String USER_ROLE = "UR:";
    public static final String USER_PERMISSION_WEB = "UPW:";
    public static final String SHARE_PERMISSION = "SP:COMMON";
    public static final String SHARE_PERMISSION_WEB = "SPW:COMMON";
    private static Random random;
    
    public static String getTokenKey(final String userId) {
        int i = RedisKeyUtils.random.nextInt(10000);
        if (i < 1000) {
            i += 1000;
        }
        return "TK:" + userId + ":" + i;
    }
    
    public static String getTokenKeyPrefix(final String userId) {
        return "TK:" + userId;
    }
    
    public static String getUserMenuKey(final String userId) {
        return "UM:" + userId;
    }
    
    public static String getUserRoleKey(final String userId) {
        return "UR:" + userId;
    }
    
    public static String getUserPermissionKey(final String userId) {
        return "UP:" + userId;
    }
    
    public static String getUserPermissionWebKey(final String userId) {
        return "UPW:" + userId;
    }
    
    public static String tokenId2base64(final String tokenId) throws UnsupportedEncodingException {
        return Base64.getEncoder().encodeToString(tokenId.getBytes("UTF-8"));
    }
    
    public static String base642tokenId(final String tokenId) throws UnsupportedEncodingException {
        if (tokenId == null || "".equals(tokenId.trim())) {
            return null;
        }
        return new String(Base64.getDecoder().decode(tokenId), "UTF-8");
    }
    
    static {
        RedisKeyUtils.random = new Random();
    }
}
